package com.echo.code.thread.atomic;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicIntegerArray;

/**
 * AtomicIntegerArrayExam Created by baiyap on 2018/10/10.
 */
public class AtomicIntegerArrayExam {

  public static void main(String[] args) throws InterruptedException {
    AtomicIntegerArray array = new AtomicIntegerArray(10);
    array.set(0, 0);
    array.set(1, 0);
    ExecutorService service = Executors.newCachedThreadPool();
    for (int i = 0; i < 10; i++) {
      service.execute(() -> {
        for (int j = 0; j < 10000; j++) {
          array.getAndIncrement(0);
        }
        for (int j = 0; j < 10000; j++) {
          array.getAndIncrement(1);
        }
      });
    }
    service.shutdown();
    service.awaitTermination(1, TimeUnit.DAYS);
    System.out.println(array);
  }
}
